﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HML.Demo
{
    public partial class PaginationBarForm : ItemFormTemplate
    {
        public PaginationBarForm()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.Form_Load);
            this.Shown += new System.EventHandler(this.Form_Shown);
        }

        #region 实现

        public override ItemFormTemplateViewObject AsynchRequestTask(ItemFormTemplateViewObject view)
        {
            System.Threading.Thread.Sleep(300);
            view.ViewData = DemoHelper.GetDescription(AppDomain.CurrentDomain.BaseDirectory + "例子\\控件\\PaginationBarForm\\PaginationBarForm.rtf");
            return view;
        }

        public override void DataBind(ItemFormTemplateViewObject viewObject)
        {
            this.introduceBrowser.Rtf = viewObject.ViewData.ToString();
        }

        public override void ShowView()
        {
            base.ShowView();
        }

        public override void HideView()
        {
            base.HideView();
        }

        public override void CloseView()
        {
            base.CloseView();
        }

        #endregion

        #region 属性面板

        private void Form_Load(object sender, EventArgs e)
        {
            this.controlContainer.SizeChanged += this.ControlContainer_SizeChanged;

            this.demo_control.SetPagination(7, 600, 10, 7);
            this.demo_control.Template = "当前共有[*]条|首页|上一页|...|...|下一页|尾页|共[*]页";
            this.demo_control.FLButtonVisible = true;
            this.demo_control.PNButtonVisible = true;
            this.demo_control.GButtonVisible = true;
            this.demo_control.PageDescriptionVisible = true;
            this.demo_control.PaginationChanged += this.Demo_control_PaginationChanged;

            this.styleText_textBox.Text = this.demo_control.Template;
            this.styleText_textBox.LostFocus += this.StyleText_textBox_LostFocus;

            this.firstLastButtonVisible_toggleSwitch.IsOn = this.demo_control.FLButtonVisible;
            this.firstLastButtonVisible_toggleSwitch.Toggled += this.FLButtonVisible_toggleSwitch_Toggled;

            this.prevNextButtonVisible_toggleSwitch.IsOn = this.demo_control.PNButtonVisible;
            this.prevNextButtonVisible_toggleSwitch.Toggled += this.PNButtonVisible_toggleSwitch_Toggled;

            this.prevNextGroupButtonVisible_toggleSwitch.IsOn = this.demo_control.GButtonVisible;
            this.prevNextGroupButtonVisible_toggleSwitch.Toggled += this.GButtonVisible_toggleSwitch_Toggled;

            this.totalRecords_toggleSwitch.IsOn = this.demo_control.RecordDescriptionVisible;
            this.totalRecords_toggleSwitch.Toggled += this.TotalRecords_toggleSwitch_Toggled;

            this.pageDescriptionVisible_toggleSwitch.IsOn = this.demo_control.PageDescriptionVisible;
            this.pageDescriptionVisible_toggleSwitch.Toggled += this.PageDescriptionVisible_toggleSwitch_Toggled;

            this.limitPage_label.Text = this.demo_control.LimitPage.ToString();
            this.limitPage_trackBarPlus.Value = this.demo_control.LimitPage;
            this.limitPage_trackBarPlus.ValueChanged += this.LimitPage_trackBarPlus_ValueChanged; 

            this.totalRecords_label.Text = this.demo_control.TotalRecords.ToString();
            this.totalRecords_trackBarPlus.Value = this.demo_control.TotalRecords;
            this.totalRecords_trackBarPlus.ValueChanged += this.TotalRecords_trackBarPlus_ValueChanged;

            this.pageSize_label1.Text = this.demo_control.PageSize.ToString();
            this.pageSize_trackBarPlus.Value = this.demo_control.PageSize;
            this.pageSize_trackBarPlus.ValueChanged += this.PageSize_trackBarPlus_ValueChanged;

            this.currentPage_label.Text = this.demo_control.CurrentPage.ToString();
            this.currentPage_trackBarPlus.Value = this.demo_control.CurrentPage;
            this.currentPage_trackBarPlus.ValueChanged += this.CurrentPage_trackBarPlus_ValueChanged;

        }

        private void Form_Shown(object sender, EventArgs e)
        {
            this.splitContainer.SplitterDistance = Math.Max(this.splitContainer.Width / 4, this.splitContainer.Width - MainForm.GlobalSplitWidth);
            this.splitContainer.SplitterMoved += this.SplitContainer_SplitterMoved;

            MainForm.LoadAssistTabControl(this.assistTabControl);
        }

        private void SplitContainer_SplitterMoved(object sender, SplitterEventArgs e)
        {
            SplitContainer control = (SplitContainer)sender;
            MainForm.GlobalSplitWidth = this.splitContainer.Width - control.SplitterDistance;
        }

        private void ControlContainer_SizeChanged(object sender, EventArgs e)
        {
            Panel control = (Panel)sender;
            this.demo_control.Location = new Point((control.Width - this.demo_control.Width) / 2, (control.Height - this.demo_control.Height) / 2);
        }

        private void Demo_control_PaginationChanged(object sender, PaginationBarChangedEventArgs e)
        {
            PaginationBar control = (PaginationBar)sender;
            control.SetPagination(control.LimitPage, control.TotalRecords, control.PageSize, e.Index);
        }

        private void StyleText_textBox_LostFocus(object sender, EventArgs e)
        {
            TextBox control = (TextBox)sender;
            this.demo_control.Template = control.Text.Trim();
        }

        private void FLButtonVisible_toggleSwitch_Toggled(object sender, EventArgs e)
        {
            ToggleSwitch control = (ToggleSwitch)sender;
            this.demo_control.FLButtonVisible = control.IsOn;
        }

        private void PNButtonVisible_toggleSwitch_Toggled(object sender, EventArgs e)
        {
            ToggleSwitch control = (ToggleSwitch)sender;
            this.demo_control.PNButtonVisible = control.IsOn;
        }

        private void GButtonVisible_toggleSwitch_Toggled(object sender, EventArgs e)
        {
            ToggleSwitch control = (ToggleSwitch)sender;
            this.demo_control.GButtonVisible = control.IsOn;
        }

        private void TotalRecords_toggleSwitch_Toggled(object sender, EventArgs e)
        {
            ToggleSwitch control = (ToggleSwitch)sender;
            this.demo_control.RecordDescriptionVisible = control.IsOn;
        }

        private void PageDescriptionVisible_toggleSwitch_Toggled(object sender, EventArgs e)
        {
            ToggleSwitch control = (ToggleSwitch)sender;
            this.demo_control.PageDescriptionVisible = control.IsOn;
        }

        private void LimitPage_trackBarPlus_ValueChanged(object sender, EventArgs e)
        {
            this.SetControlInfo();
        }

        private void TotalRecords_trackBarPlus_ValueChanged(object sender, EventArgs e)
        {
            this.SetControlInfo();
        }

        private void PageSize_trackBarPlus_ValueChanged(object sender, EventArgs e)
        {
            this.SetControlInfo();
        }

        private void CurrentPage_trackBarPlus_ValueChanged(object sender, EventArgs e)
        {
            this.SetControlInfo();
        }

        private void SetControlInfo()
        {
            int limitPage = (int)this.limitPage_trackBarPlus.Value;
            int totalRecords = (int)this.totalRecords_trackBarPlus.Value;
            int pageSize = (int)this.pageSize_trackBarPlus.Value;
            if (totalRecords == 0)
            {
                this.currentPage_trackBarPlus.MinValue = 0;
            }
            else
            {
                this.currentPage_trackBarPlus.MinValue = 1d;
            }
            this.currentPage_trackBarPlus.MaxValue = (int)Math.Ceiling(totalRecords / (float)pageSize);
            int currentPage = (int)this.currentPage_trackBarPlus.Value;
            this.demo_control.SetPagination(limitPage, totalRecords, pageSize, currentPage);

            this.limitPage_label.Text = limitPage.ToString();
            this.totalRecords_label.Text = totalRecords.ToString();
            this.pageSize_label1.Text = pageSize.ToString();
            this.currentPage_label.Text = currentPage.ToString();
        }

        #endregion

    }
}
